package com.example.ribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author yangjianrong
 * @date 2020/7/3 14:43
 **/
@RestController
public class OneController {

    @Value("${server.port}")
    String port;
    /**
     *@LoadBalanced 注解的功能可以实现客户端负载均衡的作用，
     * 也就是说，如果访问的接口服务是集群的话，可以通过轮询的方式进行访问
     */

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "error")
    @RequestMapping(value = "/test")
    public String test(){
        return restTemplate.getForObject("http://provider/test",String.class);
    }

    @HystrixCommand(fallbackMethod = "error")
    @RequestMapping("/testtwo")
    public String test1() {
        return restTemplate.getForObject("http://provider/test1",String.class);
    }

    @HystrixCommand(fallbackMethod = "error")
    @RequestMapping(value = "/testone")
    public String testOne(@RequestParam String msg){
        return restTemplate.getForObject("http://provider/testone",String.class);
    }

    /**
     * 对应的参数个数及类型需要与原方法一致
     */
    public String error(String msg) {
        return msg+"=>error";
    }
    public String error() {
        return port+"test 方法 =>error";
    }


}
